package com.oig.sys.file.service.impl;

import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.file.dao.ITbBusFileConfigDao;
import com.oig.sys.file.dto.BusFileConfigDto;
import com.oig.sys.file.entity.TbBusFileConfig;
import com.oig.sys.file.service.ITbBusFileConfigService;
import com.oig.sys.file.vo.BusFileConfigVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author wuxl
 */
@Slf4j
@Service
public class TbBusFileConfigServiceImpl implements ITbBusFileConfigService {

	private final ITbBusFileConfigDao tbBusFileConfigDao;

	@Autowired
	public TbBusFileConfigServiceImpl(ITbBusFileConfigDao tbBusFileConfigDao){
		 this.tbBusFileConfigDao = tbBusFileConfigDao;
	}

	@Transactional
	@Override
	public BusFileConfigVo addBusFileConfig(BusFileConfigDto configDto) {
		TbBusFileConfig oldConfig = this.tbBusFileConfigDao.getByBusAndRel(configDto.getBusinessType(), configDto.getRelationType()) ;
		if (oldConfig!=null){
			throw new BadRequestException("该业务附件存储位置已配置，不需要重复配置") ;
		}
		TbBusFileConfig config = new TbBusFileConfig();
		BeanUtils.copyProperties(configDto, config, "id");
		if (CommonEnum.FlagEnum.TRUE_FLAG.getCode().equals(configDto.getReqType())){
			config.setStatus(CommonEnum.CommonStatusEnum.CHECKING.getCode());
		} else {
			config.setStatus(CommonEnum.CommonStatusEnum.INIT.getCode());
		}
		this.tbBusFileConfigDao.save(config);
		return this.buildVo(config);
	}

	private BusFileConfigVo buildVo(TbBusFileConfig config){
		BusFileConfigVo configVo = new BusFileConfigVo();
		BeanUtils.copyProperties(config,configVo);
		return configVo;
	}



	@Transactional
	@Override
	public BusFileConfigVo updateBusFileConfig(BusFileConfigDto configDto) {
		TbBusFileConfig config = this.tbBusFileConfigDao.getBusFileConfigById(configDto.getId()) ;
		if (CommonEnum.CommonStatusEnum.OK.getCode().equals(config.getStatus())
				||CommonEnum.CommonStatusEnum.CHECKING.getCode().equals(config.getStatus())){
			throw new BadRequestException("生效后不能修改，请重新配置") ;
		}
		BeanUtils.copyProperties(configDto, config, "id", "systemId", "systemName","createId","createName","createTime");
		if (CommonEnum.FlagEnum.TRUE_FLAG.getCode().equals(configDto.getReqType())){
			config.setStatus(CommonEnum.CommonStatusEnum.CHECKING.getCode());
		} else {
			config.setStatus(CommonEnum.CommonStatusEnum.INIT.getCode());
		}
		this.tbBusFileConfigDao.save(config);
		return this.buildVo(config);
	}

	@Transactional
	@Override
	public BusFileConfigVo auditBusFileConfig(BusFileConfigDto configDto) {
		TbBusFileConfig config = this.tbBusFileConfigDao.getBusFileConfigById(configDto.getId()) ;
		if (!CommonEnum.CommonStatusEnum.CHECKING.getCode().equals(config.getStatus())){
			throw new BadRequestException("非待复核状态") ;
		}
		if (CommonEnum.FlagEnum.TRUE_FLAG.getCode().equals(configDto.getReqType())){
			config.setStatus(CommonEnum.CommonStatusEnum.OK.getCode());
		} else {
			config.setStatus(CommonEnum.CommonStatusEnum.UN_ENABLE.getCode());
		}
		config.setCheckRemark(configDto.getCheckRemark());
		config.setCheckId(configDto.getCheckId());
		config.setCheckName(configDto.getCheckName());
		config.setCheckTime(configDto.getCheckTime());
		this.tbBusFileConfigDao.save(config);
		return this.buildVo(config);
	}

	@Transactional
	@Override
	public void cancelBusFileConfig(Integer id) {
		TbBusFileConfig config = this.tbBusFileConfigDao.getBusFileConfigById(id) ;
		config.setStatus(CommonEnum.CommonStatusEnum.DEL.getCode());
		this.tbBusFileConfigDao.save(config);
	}


	@Override
	public BusFileConfigVo getBusFileConfig(Integer id) {
		TbBusFileConfig config = this.tbBusFileConfigDao.getBusFileConfigById(id) ;
		return this.buildVo(config);
	}


	@Override
	public BusFileConfigVo findConfig(String businessType, String relationType) {
		TbBusFileConfig config = this.tbBusFileConfigDao.getByBusAndRel(businessType, relationType);
		if (config==null||!CommonEnum.CommonStatusEnum.OK.getCode().equals(config.getStatus())){
			log.error("businessType={} relationType={} 未配置bucket或者配置未生效", businessType, relationType);
			throw new BadRequestException("未配置bucket或者配置未生效");
		}
		return this.buildVo(config);
	}
}
